package com.jhon.yinji.lichuanvip.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.jhon.yinji.lichuanvip.R;
import com.jhon.yinji.lichuanvip.adapter.MarginDecoration;
import com.jhon.yinji.lichuanvip.adapter.MyAutoScrollAdapter;
import com.jhon.yinji.lichuanvip.adapter.NumberedAdapter;
import com.jhon.yinji.lichuanvip.bean.Conf;
import com.jhon.yinji.lichuanvip.bean.Shipin;
import com.jhon.yinji.lichuanvip.view.AutoScrollViewPager;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yinji on 2016/7/14.
 */
@ContentView(R.layout.fragment_main)
public class MainFragment extends BaseFragment {
    @ViewInject(R.id.am_asvp)
    private AutoScrollViewPager am_asvp;
    @ViewInject(R.id.fm_rv)
    private RecyclerView fm_rv;
    private List<Shipin> shipins = new ArrayList<Shipin>();
    ;
    private MyAutoScrollAdapter myAutoScrollAdapter;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        myAutoScrollAdapter = new MyAutoScrollAdapter(shipins);
        am_asvp.setAdapter(myAutoScrollAdapter);
        am_asvp.setInterval(3000);
        am_asvp.startAutoScroll();
        initData();
        fm_rv.addItemDecoration(new MarginDecoration(getActivity()));
        fm_rv.setHasFixedSize(true);
        fm_rv.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        String[] array = getResources().getStringArray(R.array.labels);
        fm_rv.setAdapter(new NumberedAdapter(array));
    }

    private void initData() {
        RequestParams params = new RequestParams(Conf.BASE_URL);
        JsonObject object = new JsonObject();
        object.addProperty("actionName", "2");
        object.addProperty("encrypt", "none");
        object.addProperty("imsi", Conf.IMSI);
        params.addQueryStringParameter("paramMap", object.toString());
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                JsonObject object = new JsonParser().parse(result).getAsJsonObject();
                if (object.get("list") != null) {
                    JsonArray jsonArray = object.get("list").getAsJsonArray();
                    shipins.clear();
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JsonElement jsonElement = jsonArray.get(i);
                        Shipin shipin = gson.fromJson(jsonElement.getAsJsonObject(), Shipin.class);
                        shipins.add(shipin);
                    }
                    myAutoScrollAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
